﻿Public Class FrmPlanningBLigne
    Public pNoLigne As Integer
    Public pCD_MON As String
    Public pDateTime As Date

    Private szSQL As String
    Private rs As ADODB.Recordset
    Dim old_CD_CLI As Integer = 0
    Dim nHeureSpeciale As Integer

    Private Sub FrmPlanningBLigne_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        InitComboStatutsLecon(CB_STATUT)
        RdVToScreen()
        ' si heure eleve, interdire bouton "Duppliquer"
        If TE_CD_CLI.TextLength = 6 Then
            BO_DUPLICATE.Enabled = False
        Else
            BO_DUPLICATE.Enabled = True
        End If
        If TE_CD_CLI.TextLength > 0 Then
            TE_QTE.Enabled = False
        Else
            TE_QTE.Enabled = True
        End If

    End Sub

    Private Sub RdVToScreen()
        Dim rsEleve As ADODB.Recordset = Nothing

        If pNoLigne = 0 Then
            ClearTextBoxes(Me)
            TE_REF.Text = "HCB"
            TE_LETTRE.Text = ""
            TE_LIEU_RDV.Text = "Rendez-vous à votre agence"
            setIntituleChkBox(0)
            chkParTelephone.Checked = False
            TE_QTE.Value = 1
            TE_DATETIME.Value = pDateTime.Date
            TE_DATETIME.Value = TE_DATETIME.Value.AddHours(pDateTime.Hour)
            BO_ENREGISTRER.Enabled = True
            BO_RECHERCHER.Enabled = True
            BO_DISPO.Enabled = True
            TE_COMMENT.Enabled = True
        Else
            ' Lire le rendez-vous sélectionné
            szSQL = "CALL spGet_ELEVES_CPT_byNO_LIGNE(" & pNoLigne & ")"
            GetRecordset(ctDSN_AEM, szSQL, rs)
            If rs.RecordCount > 0 Then
                TE_CD_CLI.Text = rs.Fields("cd_cli").Value
                gCD_CLI = TE_CD_CLI.Text
                TE_DATETIME.Value = rs.Fields("date").Value
                TE_DATETIME.Value = TE_DATETIME.Value.AddHours(rs.Fields("noplan").Value) ' heure du rdv
                TE_CD_MON.Text = rs.Fields("cd_mon").Value
                TE_NOM.Text = nv(rs.Fields("nom").Value)
                ' Lire la fiche Eleves
                szSQL = "CALL spGet_ELEVES_byCD_CLI(" & TE_CD_CLI.Text & ")"
                GetRecordset(ctDSN_AEM, szSQL, rsEleve)
                If rsEleve.RecordCount > 0 Then
                    TE_FORMULE.Text = nv(rsEleve.Fields("formule").Value)
                    TE_OBS.Text = nv(rsEleve.Fields("obs").Value)
                    rsEleve.Close()
                Else
                    TE_FORMULE.Text = " "
                    TE_OBS.Text = " "
                End If
                CB_STATUT.SelectedIndex = CB_STATUT.FindString(rs.Fields("statut").Value.ToString)
                TE_QTE.Value = rs.Fields("qte").Value
                TE_REF.Text = nv(rs.Fields("ref").Value)
                TE_INTITULE.Text = nv(rs.Fields("intitule").Value)
                setIntituleChkBox(rs.Fields("circuit").Value)
                chkParTelephone.Checked = rs.Fields("partelephone").Value
                TE_COMMENT.Text = nv(rs.Fields("comment").Value)
                TE_LIEU_RDV.Text = nv(rs.Fields("lieu_rdv").Value)
                TE_LETTRE.Text = rs.Fields("lettre").Value
                TE_MODIFIE_LE.Text = setModifiedBy(rs.Fields("cree_le").Value, rs.Fields("modif_le").Value, rs.Fields("cd_uti").Value)
                ' rdv déjà validé ?
                If IsDate(rs.Fields("date_valeur").Value) Then
                    BO_ENREGISTRER.Enabled = False
                    BO_RECHERCHER.Enabled = False
                    BO_DISPO.Enabled = False
                Else
                    If gUserAcces < 2 Then
                        BO_ENREGISTRER.Enabled = False
                        BO_RECHERCHER.Enabled = False
                        BO_DISPO.Enabled = False
                    Else
                        BO_ENREGISTRER.Enabled = True
                        BO_RECHERCHER.Enabled = True
                        BO_DISPO.Enabled = True
                    End If
                End If
                ' si activité
                If IsEleve(rs.Fields("cd_cli").Value) = False Then
                    old_CD_CLI = 0
                    GB_HEURES_SPECIALES.Enabled = False
                Else
                    old_CD_CLI = gCD_CLI
                    GB_HEURES_SPECIALES.Enabled = True
                End If
                ' bloquer la saisie du commentaire si RESA L ou RESA P
                If gCD_CLI = 99998 Or gCD_CLI = 99999 Then
                    TE_COMMENT.Clear()
                    TE_COMMENT.Enabled = False
                Else
                    TE_COMMENT.Enabled = True
                End If
            End If
            rs.Close()
        End If
        Me.Text = "PLANNING - " & TE_DATETIME.Text

    End Sub

    Private Function IsEleveOK() As Boolean

        IsEleveOK = False
        If TE_CD_CLI.TextLength = 0 Then
            Exit Function
        End If

        ' effectuer les controles sur l'élève
        If pCD_MON = "9ZZ" Then
            ' si moniteur evaluation 
            If ControlerEleves("EVA", TE_CD_CLI.Text) = False Then
                Exit Function
            End If
        Else
            ' si autre moniteur
            If ControlerEleves("PLB", TE_CD_CLI.Text) = False Then
                Exit Function
            End If
        End If

        gCD_CLI = TE_CD_CLI.Text
        ' controler heures en doubles ?
        If pCD_MON.Length And IsEleve(gCD_CLI) Then
            szSQL = "CALL spGet_ELEVES_CPT_byCD_CLI(" & gCD_CLI & "," & TE_DATETIME.Value.Hour & ",'" & Format(TE_DATETIME.Value, "yyyy/MM/dd") & "')"
            GetRecordset(ctDSN_AEM, szSQL, rs)
            If rs.RecordCount > 0 Then
                If pCD_MON <> rs.Fields("cd_mon").Value Then
                    MsgBox("L'élève a déjà un rendez-vous la même heure avec le moniteur " & rs.Fields("cd_mon").Value)
                    rs.Close()
                    Exit Function
                End If
            End If
        End If

        IsEleveOK = True

    End Function

    Private Sub BO_RECHERCHER_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_RECHERCHER.Click

        ' si Elève provisoire, proposer le dernier code client consulté
        If TE_CD_CLI.TextLength Then
            gCD_CLI = TE_CD_CLI.Text
        End If
        ' ouvre la fenetre de Recherche ELEVES
        frmElevesSearch.ShowDialog()

        ' ancien cd_cli est de type élève, le nouveau doit être également élève
        If old_CD_CLI > 100000 And gCD_CLI < 100000 Then
            MsgBox("Vous devenez sélectionner un élève !", MsgBoxStyle.Critical)
            Exit Sub
        End If
        TE_CD_CLI.Text = gCD_CLI
        TE_NOM.Text = frmElevesSearch.Nompren
        TE_FORMULE.Text = frmElevesSearch.Formule
        ' bloquer la saisie du commentaire si RESA L ou RESA P
        If gCD_CLI = 99998 Or gCD_CLI = 99999 Then
            TE_COMMENT.Clear()
            TE_COMMENT.Enabled = False
        Else
            TE_COMMENT.Enabled = True
        End If
        ' activite ? oui, ref = code élève 
        If IsEleve(gCD_CLI) = False Then
            TE_REF.Text = gCD_CLI
            TE_INTITULE.Text = frmElevesSearch.Nompren
            BO_DUPLICATE.Enabled = True
        Else
            TE_REF.Text = "HCB"
            BO_DUPLICATE.Enabled = False
        End If
        ' passe au champs suivant
        chkParTelephone.Focus()

    End Sub

    Private Sub BO_ELEVE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_ELEVE.Click
        ' affiche la fiche de l'éleve
        If TE_CD_CLI.TextLength Then
            gCD_CLI = TE_CD_CLI.Text
            If IsEleve(gCD_CLI) Then
                frmElevesMAJ.ShowDialog()
            End If
        End If
    End Sub

    Private Sub setIntituleChkBox(ByVal nType As Integer)
        ' mémoriser la valeur
        nHeureSpeciale = nType
        ' déchocher toutes les cases
        rb_HCB.Checked = False
        rb_TestBlanc.Checked = False
        rb_Prealable.Checked = False
        rb_Pedag_1.Checked = False
        rb_Pedag_2.Checked = False
        rb_Evaluation.Checked = False
        ' puis cocher la case appropriée
        Select Case nType
            Case 0
                rb_HCB.Checked = True

            Case 1
                rb_TestBlanc.Checked = True

            Case 2
                rb_Prealable.Checked = True

            Case 3
                rb_Pedag_1.Checked = True

            Case 4
                rb_Evaluation.Checked = True

            Case 5
                rb_Pedag_2.Checked = True
        End Select
    End Sub

    Private Sub rb_HCB_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rb_HCB.CheckedChanged
        TE_INTITULE.Text = "HEURE CONDUITE B"
        TE_LIEU_RDV.Text = "Rendez-vous à votre agence"
        nHeureSpeciale = 0
    End Sub

    Private Sub rb_TestBlanc_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rb_TestBlanc.CheckedChanged
        TE_INTITULE.Text = "EXAMEN BLANC"
        TE_LIEU_RDV.Text = "Rendez-vous à votre agence"
        nHeureSpeciale = 1
    End Sub

    Private Sub rb_Prealable_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rb_Prealable.CheckedChanged
        TE_INTITULE.Text = "RDV PREALABLE"
        TE_LIEU_RDV.Text = "Présence de l'accompagnateur OBLIGATOIRE"
        nHeureSpeciale = 2
    End Sub

    Private Sub rb_Pedag_1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rb_Pedag_1.CheckedChanged
        TE_INTITULE.Text = "RDV PEDAGOGIQUE 1"
        TE_LIEU_RDV.Text = "Présence de l'accompagnateur OBLIGATOIRE"
        nHeureSpeciale = 3
    End Sub

    Private Sub rb_Evaluation_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rb_Evaluation.CheckedChanged
        TE_INTITULE.Text = "EVALUATION"
        TE_LIEU_RDV.Text = "Rendez-vous à votre agence"
        nHeureSpeciale = 4
    End Sub

    Private Sub rb_Pedag_2_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rb_Pedag_2.CheckedChanged
        TE_INTITULE.Text = "RDV PEDAGOGIQUE 2"
        TE_LIEU_RDV.Text = "Présence de l'accompagnateur OBLIGATOIRE"
        nHeureSpeciale = 5
    End Sub

    Private Sub BO_ENREGISTRER_Click(sender As System.Object, e As System.EventArgs) Handles BO_ENREGISTRER.Click

        If IsEleveOK() = False Then
            TE_CD_CLI.Focus()
            Exit Sub
        End If
        ' si moniteur <> evaluation, 
        If pCD_MON <> "9ZZ" Then
            ' controler le solde de l'élève
            If ControlerSolde("REEL", TE_CD_CLI.Text) = False Then
                TE_CD_CLI.Focus()
                Exit Sub
            End If
        End If

        If pNoLigne = 0 Then
            ' insérer une heure 
            szSQL = "CALL spINS_PLANNING_B(" & TE_CD_CLI.Text & ",'" & TE_INTITULE.Text & "','" & Format(TE_DATETIME.Value, "yyyy/MM/dd") & "'," & TE_DATETIME.Value.Hour & ",1,'" & _
                        TE_REF.Text & "','" & pCD_MON & "'," & gUserAgence & ",'" & gUserCode & "')"
            GetRecordset(ctDSN_AEM, szSQL, rs)
            If rs.RecordCount > 0 Then
                pNoLigne = rs.Fields("last_id").Value
            End If
            rs.Close()
        End If
        ' mise à jour le rdv
        szSQL = "CALL spUpd_PLANNING_B(" & pNoLigne & "," & TE_CD_CLI.Text & ",'" & nq(TE_NOM.Text) & "'," & _
                 TE_QTE.Value & "," & CInt(chkParTelephone.Checked) & ",'" & TE_REF.Text & "','" & nq(TE_LIEU_RDV.Text) & "','" & _
                 nq(TE_COMMENT.Text) & "'," & getCodeFromCombo(CB_STATUT.SelectedItem) & "," & nHeureSpeciale & ",'" & gUserCode & "')"
        If ExecQuery(ctDSN_AEM, szSQL) Then
            UpdatePlanningB(getCodeFromCombo(CB_STATUT.SelectedItem), pNoLigne, TE_INTITULE.Text)
            ' si changement de code élève, marque l'ancien rdv comme remplacé
            If old_CD_CLI > 0 And TE_CD_CLI.Text <> old_CD_CLI.ToString Then
                szSQL = "CALL spINS_PLANNING_B(" & old_CD_CLI & ",'RDV de " & TE_DATETIME.Value.Hour & " h remplacé','" & Format(TE_DATETIME.Value, "yyyy/MM/dd") & _
                     "',0,0,'','" & pCD_MON & "'," & gUserAgence & ",'" & gUserCode & "')"
                ExecQuery(ctDSN_AEM, szSQL)
            End If
            Me.Close()
        End If

    End Sub

    Private Sub BO_CARNET_RDV_Click(sender As System.Object, e As System.EventArgs) Handles BO_CARNET_RDV.Click
        ' imprimer le carnet de rdv B
        FrmViewReport.pCodeEtat = 22
        FrmViewReport.pParamRPT = ""
        FrmViewReport.ShowDialog()
    End Sub

    Private Sub BO_DUPLICATE_Click(sender As System.Object, e As System.EventArgs) Handles BO_DUPLICATE.Click
        rb_UNE_FOIS.Checked = True
        GB_DUPPLICATE.Visible = True
    End Sub

    Private Sub BO_DUPPLIQUER_Click(sender As System.Object, e As System.EventArgs) Handles BO_DUPPLIQUER.Click

        ' enregistrer le rdv
        BO_ENREGISTRER_Click(sender, e)

        ' duppliquer la ligne de compte
        If rb_UNE_FOIS.Checked = True Then
            szSQL = "CALL spINS_PLANNING_B_DUP(" & pNoLigne & ",7)"
        Else
            szSQL = "CALL spINS_PLANNING_B_DUP(" & pNoLigne & "," & TE_NB_FOIS.Value & ")"
        End If
        ExecQuery(ctDSN_AEM, szSQL)
        GB_DUPPLICATE.Visible = False

    End Sub

    Private Sub rb_UNE_FOIS_CheckedChanged(sender As Object, e As System.EventArgs) Handles rb_UNE_FOIS.CheckedChanged
        If rb_UNE_FOIS.Checked = True Then
            TE_NB_FOIS.Enabled = False
        Else
            TE_NB_FOIS.Enabled = True
        End If
    End Sub

    Private Sub BO_ANNULER_Click(sender As System.Object, e As System.EventArgs) Handles BO_ANNULER.Click
        GB_DUPPLICATE.Visible = False
    End Sub

    Private Sub BO_DISPO_Click(sender As System.Object, e As System.EventArgs) Handles BO_DISPO.Click
        ' ouvre la fenetre de Recherche ELEVES disponible
        frmElevesDispo.pPlanning = "B"
        frmElevesDispo.pMoniteur = pCD_MON
        frmElevesDispo.pDateHeure = TE_DATETIME.Value
        frmElevesDispo.ShowDialog()
        TE_CD_CLI.Text = gCD_CLI
        TE_NOM.Text = frmElevesDispo.pNompren
        TE_FORMULE.Text = frmElevesDispo.pFormule
        ' bloquer la saisie du commentaire si RESA L ou RESA P
        If gCD_CLI = 99998 Or gCD_CLI = 99999 Then
            TE_COMMENT.Clear()
            TE_COMMENT.Enabled = False
        Else
            TE_COMMENT.Enabled = True
        End If

        TE_REF.Text = "HCB"
        BO_DUPLICATE.Enabled = False
        ' passe au champs suivant
        chkParTelephone.Focus()

    End Sub
End Class